package com.youlu.campus.service.ca;

import com.youlu.campus.entity.UserCaReceiveRecord;
import com.youlu.campus.entity.enums.CaMaterialType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class UserCaReceiveRecordService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询用户是否领取过电子证书
     * @param userId
     * @param caType
     * @param activityId
     * @param material
     * @return
     */
    public Boolean existCaByUserIdAndCaTypeAndActivityId(String userId, String caType, String activityId,
                                                       CaMaterialType material){
        log.info("【领取电子证书】userId - > {} |caType - > {} | activityId-> {}| material - > {}",userId,caType,
                activityId,material);
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.addCriteria(Criteria.where("activityId").is(activityId));
        query.addCriteria(Criteria.where("material").is(material));
        query.addCriteria(Criteria.where("caType").is(caType));
        return this.mongoTemplate.exists(query, UserCaReceiveRecord.class);
    }

    /**
     * 查询用户是否领取过电子证书
     * @param userId
     * @param caType
     * @param activityId
     * @param caLevel
     * @param material
     * @return
     */
    public Boolean existCaByUserIdAndCaTypeAndLevelActivityId(String userId, String caType, String caLevel,String activityId,
                                                         CaMaterialType material){
        log.info("【领取电子证书】userId - > {} |caType - > {} | activityId-> {}| material - > {}",userId,caType,
                activityId,material);
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.addCriteria(Criteria.where("caType").is(caType));
        query.addCriteria(Criteria.where("caLevel").is(caLevel));
        query.addCriteria(Criteria.where("activityId").is(activityId));
        query.addCriteria(Criteria.where("material").is(material));
        return this.mongoTemplate.exists(query, UserCaReceiveRecord.class);
    }

    /**
     * 查询用户是否领取过电子证书
     * @param caUserId
     * @param caType
     * @param activityId
     * @param material
     * @return
     */
    public Boolean existByUserIdAndcaTypeAndActivityId(String caUserId, String caType, String activityId,
                                                       CaMaterialType material){
        log.info("【领取电子证书】caUserId - > {} |caType - > {} | activityId-> {}| material - > {}",caUserId,caType,
                activityId,material);
        Query query = new Query();
        query.addCriteria(Criteria.where("userCaId").is(caUserId));
        query.addCriteria(Criteria.where("activityId").is(activityId));
        query.addCriteria(Criteria.where("material").is(material));
        query.addCriteria(Criteria.where("caType").is(caType));
        return this.mongoTemplate.exists(query, UserCaReceiveRecord.class);
    }

    @Async
    public void create(UserCaReceiveRecord userCaReceiveRecord){
        log.info("【领取电子证书更新领取记录】userId - > {} |活动->{}| 等级->{} caLevel ->{}",userCaReceiveRecord.getUserId(),
                userCaReceiveRecord.getActivityId(),userCaReceiveRecord.getCaType(),userCaReceiveRecord.getCaLevel());
        Query query = new Query();
        if(StringUtils.isNotBlank(userCaReceiveRecord.getUserCaId())){
            query.addCriteria(Criteria.where("userCaId").is(userCaReceiveRecord.getUserCaId()));
        }
        if(StringUtils.isNotBlank(userCaReceiveRecord.getUserId())){
            query.addCriteria(Criteria.where("userId").is(userCaReceiveRecord.getUserId()));
        }
        query.addCriteria(Criteria.where("activityId").is(userCaReceiveRecord.getActivityId()));
        query.addCriteria(Criteria.where("material").is(userCaReceiveRecord.getMaterial()));
        query.addCriteria(Criteria.where("caType").is(userCaReceiveRecord.getCaType()));
        if(StringUtils.isNotBlank(userCaReceiveRecord.getCaLevel())){
            query.addCriteria(Criteria.where("caLevel").is(userCaReceiveRecord.getCaLevel()));
        }
        Date now = new Date();
        Update update = new Update();
        boolean exists = this.mongoTemplate.exists(query, UserCaReceiveRecord.class);
        if(!exists){
            update.set("createdTime",now);
        }
        update.set("updatedTime",now);
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.upsert(Boolean.TRUE);
        findAndModifyOptions.returnNew(Boolean.TRUE);
        this.mongoTemplate.findAndModify(query,update,findAndModifyOptions,UserCaReceiveRecord.class);
    }
}
